import EventEmitter from 'events'
import TransmuxingController from './transmuxing-controller'
import TransmuxingEvents from './transmuxing-events'

class TransMuxer {
    constructor(mediaDataSource, config) {
        this._emitter = new EventEmitter()
        this._controller = new TransmuxingController(mediaDataSource, config)

        let ctl = this._controller
        ctl.on(TransmuxingEvents.IO_ERROR, this._onIOError.bind(this))
        ctl.on(TransmuxingEvents.DEMUX_ERROR, this._onDemuxError.bind(this))
        ctl.on(TransmuxingEvents.INIT_SEGMENT, this._onInitSegment.bind(this))
        ctl.on(TransmuxingEvents.MEDIA_SEGMENT, this._onMediaSegment.bind(this))
        ctl.on(TransmuxingEvents.LOADING_COMPLETE, this._onLoadingComplete.bind(this))
        ctl.on(TransmuxingEvents.RECOVERED_EARLY_EOF, this._onRecoveredEarlyEof.bind(this))
        ctl.on(TransmuxingEvents.MEDIA_INFO, this._onMediaInfo.bind(this))
        ctl.on(TransmuxingEvents.METADATA_ARRIVED, this._onMetaDataArrived.bind(this))
        ctl.on(TransmuxingEvents.SCRIPTDATA_ARRIVED, this._onScriptDataArrived.bind(this))
        ctl.on(TransmuxingEvents.STATISTICS_INFO, this._onStatisticsInfo.bind(this))
        ctl.on(TransmuxingEvents.RECOMMEND_SEEKPOINT, this._onRecommendSeekpoint.bind(this))
    }

    on(event, listener) {
        this._emitter.addListener(event, listener);
    }

    off(event, listener) {
        this._emitter.removeListener(event, listener);
    }

    open() {
        this._controller.start()
    }
}

export default TransMuxer